package wave.robot.second;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ImgyServlet
        extends
        HttpServlet
{
    @SuppressWarnings("unchecked")
    public void doGet(
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException
    {
        Enumeration e =
                request
                        .getParameterNames();

        Map<String, String> map =
                new HashMap<String, String>();

        while (e
                .hasMoreElements())
        {
            String name =
                    (String) e
                            .nextElement();

            String value =
                    request
                            .getParameter(name);

            map
                    .put(
                            name,
                            value);
        }

        response
                .setContentType("text/calendar");

        String result =
                ""
                        + "BEGIN:VCALENDAR\n"
                        + "METHOD:PUBLISH\n"
                        + "CALSCALE:GREGORIAN\n"
                        + "BEGIN:VEVENT\n"
                        + "SUMMARY:"
                        + getStringFromMap(
                                map,
                                "event")
                        + "\n"
                        + "DESCRIPTION:"
                        + getStringFromMap(
                                map,
                                "details")
                        + "\n"
                        + "UID:WaveEventy@sandbox.com\n"
                        + "DTSTAMP:"
                        + getStringFromMap(
                                map,
                                "start")
                        + "\n"
                        + "DTSTART:"
                        + getStringFromMap(
                                map,
                                "start")
                        + "\n"
                        + "DTEND:"
                        + getStringFromMap(
                                map,
                                "end")
                        + "\n"
                        + "LOCATION:"
                        + getStringFromMap(
                                map,
                                "location")
                        + "\n"
                        + "END:VEVENT\n"
                        + "END:VCALENDAR\n";

        response
                .getWriter()
                .println(
                        result);
    }

    private String getStringFromMap(
            Map<String, String> map,
            String key)
    {
        if (!map
                .containsKey(key))
        {
            return "";
        }

        return map
                .get(
                        key)
                .toString()
                .replaceAll(
                        "\\+",
                        " ")
                .replaceAll(
                        "%3a",
                        ":")
                .replaceAll(
                        "%2f",
                        "/");
    }

}
